import cloud from '@tbmp/mp-cloud-sdk';
cloud.init({
    env: 'online'
});
class ServerRequest {
	constructor() {
	  this.system = my.getSystemInfoSync();
	  console.log("system:",this.system);
	  let str = '0123456789abcdef';
	  this.traceId = '';
	  for(let i = 0; i < 20; i ++) {
		  let idx = parseInt(Math.random() * 16);
		  this.raceId += str[idx];
	  };
	  this.tabIndex = 1;
	  this.modelIndex = 0;
	}
  
	static getInstance() {   //单例
	  if (!ServerRequest.instance) {
		console.log("create ServerRequest");
		ServerRequest.instance = new ServerRequest();
	  }
	  
	  return ServerRequest.instance;
	}
  
	setEnterTime(){
	  this.startTime = new Date().getTime();
	}
  
	getTimeOnSite(){
	  return new Date().getTime() - this.startTime
	}
  
	  /**
	 * 判断是否是iphone
	 */
	getIsIphone(){
	  const platform = this.system.platform;
	  if(platform.toLowerCase() == 'ios'){
		return true;
	  }else{
		return false;
	  }
	}
  
	/**
	 * 判断是否是全面屏
	 */
	getIsFullScreen(){
	  if(this.system.screenWidth/this.system.screenHeight<=0.51){
		return true;
	  }else{
		return false;
	  }
	}
  
	async invoke(name,handle,data={}){
	  // console.log(handle + "-invoke data:",JSON.stringify(data));
	  let result = await cloud.function.invoke(name,data,handle);
	  // console.log(handle + "-invoke result:",JSON.stringify(result));
	  if(typeof result == 'string'){
		result = JSON.parse(result);
	  }
	  return result;
	}
  
	async track(customType,type='',value=''){
	  var info = {
		version: "v3",
		traceId: this.traceId,
		projectCode: "2674",
		timestamp: new Date().getTime().toString(),
		device: this.system.platform,
		province: "",
		city: "",
		customType: customType,
		type: type,
		platform: "taobao",
		channel: "taobao",
		value,
	  }
	  
	   await this.invoke('morefun','addTrack',{ data: info});
	}
  }
  
  export default ServerRequest